<?php
namespace app\user\controller;

use app\common\controller\Front;

class Name extends Front
{
    protected $auth = [
         'check'       => true,
         'none_login'  => '',
         'none_right'  => '*',
         'error_login' => 'user/login/index',
         'error_right' => 'user/login/index',
    ];
    
	public function _initialize()
    {
        $this->request->filter('trim,strip_tags,htmlspecialchars');
        
		parent::_initialize();
	}
    
	public function index()
    {
		return $this->fetch();
	}
    
    public function update()
    {
        $post = input('post.');
        $validate = new \think\Validate([
            'user_name' => 'length:3,30',
        ],[
            'user_name.length' => lang('error_length'),
        ]);
        if(!$validate->check($post)){
            $this->error($validate->getError());
        }
        //验证唯一
        if(db('user')->where('user_nice_name','eq',$post['user_name'])->value('user_id')){
            $this->error(lang('error_name_repeat'));
        }
        //修改呢称
        $result = db('user')->update([
            'user_id'        => $this->site['user']['user_id'],
            'user_nice_name' => $post['user_name'],
        ]);
        if(!$result){
            $this->error(db('user')->getError());
        }
        //操作成功
        $this->success(lang('success'), 'user/center/index', ['action'=>'update']);
    }
}